﻿// <copyright file="WebControlExtensions.cs" company="Edge Extensions Project">
// Copyright (c) 2009 All Rights Reserved
// </copyright>
// <author>Kevin Nessland</author>
// <email>kevinnessland@gmail.com</email>
// <date>2009-07-08</date>
// <summary>Contains WebControl-related extension methods.</summary>
namespace Edge.Extensions.Web
{
    using System.Web.UI;

    /// <summary>
    /// Provides extension methods to the <see cref="System.Web.UI">System.Web.UI</see> object.
    /// </summary>
    public static class WebControlExtensions
    {
        /// <summary>
        /// Extension method to recursively look for the specified control.
        /// </summary>
        /// <param name="root">The root object where to start from.</param>
        /// <param name="id">Identifier of the control to look for.</param>
        /// <returns>A reference to the specified <see cref="Control">Control</see>.</returns>
        public static Control FindControlRecursive(this Control root, string id)
        {
            Control objControlFound = null;

            if ((root == null) == false)
            {
                objControlFound = root.FindControl(id);

                if ((objControlFound == null) == true)
                {
                    foreach (Control objControl in root.Controls)
                    {
                        objControlFound = objControl.FindControlRecursive(id);

                        if ((objControlFound == null) == false)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                objControlFound = null;
            }

            return objControlFound;
        }
    }
}
